package gui;
/**
 * @author  Benjamin Lee
 * @created November 16, 2011
 * @email benjamin.lee@my.uwrf.edu
 * 
 */
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;
public class Panel extends JPanel implements ActionListener {

	/**
	 * GUI element declaration. An element in this case is a part of the GUI
	 * such as a button, frame, or text box.
	 */
	JPanel pnPanel2;
	JButton btBut4;
	JButton btBut5;
	JButton btBut6;
	JButton btBut7;
	JTextField tfText2;
	JLabel lbLabel4;
	String Question = "";

	/**
	 * Default constructor that creates the GUI. More details inside the
	 * constructor.
	 */
	public Panel() {
		super();
		int Total=0;
		int Quiz=0;
		setBorder(BorderFactory.createTitledBorder("Let's Play!"));

		GridBagLayout gbPanel0 = new GridBagLayout();
		GridBagConstraints gbcPanel0 = new GridBagConstraints();
		setLayout(gbPanel0);

		pnPanel2 = new JPanel();
		GridBagLayout gbPanel2 = new GridBagLayout();
		GridBagConstraints gbcPanel2 = new GridBagConstraints();
		pnPanel2.setLayout(gbPanel2);

		/**
		 * Submit answer button. It also sets up physical dimensions of the
		 * button.
		 */
		btBut4 = new JButton("Submit Answer");
		btBut4.addActionListener(this);
		gbcPanel2.gridx = 1;
		gbcPanel2.gridy = 11;
		gbcPanel2.gridwidth = 6;
		gbcPanel2.gridheight = 4;
		gbcPanel2.fill = GridBagConstraints.BOTH;
		gbcPanel2.weightx = 1;
		gbcPanel2.weighty = 0;
		gbcPanel2.anchor = GridBagConstraints.NORTH;
		gbPanel2.setConstraints(btBut4, gbcPanel2);
		pnPanel2.add(btBut4);
		// End Session Button
		
		btBut5 = new JButton("See Score");
		btBut5.addActionListener(this);
		gbcPanel2.gridx = 8;
		gbcPanel2.gridy = 11;
		gbcPanel2.gridwidth = 6;
		gbcPanel2.gridheight = 4;
		gbcPanel2.fill = GridBagConstraints.BOTH;
		gbcPanel2.weightx = 1;
		gbcPanel2.weighty = 0;
		gbcPanel2.anchor = GridBagConstraints.NORTH;
		gbPanel2.setConstraints(btBut5, gbcPanel2);
		pnPanel2.add(btBut5);

		/**
		 * Start game button. It also sets up physical dimensions of the button.
		 */
		btBut6 = new JButton("Start");
		btBut6.addActionListener(this);
		gbcPanel2.gridx = 5;
		gbcPanel2.gridy = 15;
		gbcPanel2.gridwidth = 5;
		gbcPanel2.gridheight = 2;
		gbcPanel2.fill = GridBagConstraints.BOTH;
		gbcPanel2.weightx = 1;
		gbcPanel2.weighty = 0;
		gbcPanel2.anchor = GridBagConstraints.NORTH;
		gbPanel2.setConstraints(btBut6, gbcPanel2);
		pnPanel2.add(btBut6);
		gbcPanel0.gridx = 1;
		gbcPanel0.gridy = 0;
		gbcPanel0.gridwidth = 19;
		gbcPanel0.gridheight = 19;
		gbcPanel0.fill = GridBagConstraints.BOTH;
		gbcPanel0.weightx = 1;
		gbcPanel0.weighty = 0;
		gbcPanel0.anchor = GridBagConstraints.NORTH;
		gbPanel0.setConstraints(pnPanel2, gbcPanel0);
		add(pnPanel2);

		/**
		 * Exit game button. It also sets up physical dimensions of the button.
		 */
		btBut7 = new JButton("Exit Game");
		btBut7.addActionListener(this);
		gbcPanel2.gridx = 5;
		gbcPanel2.gridy = 17;
		gbcPanel2.gridwidth = 5;
		gbcPanel2.gridheight = 2;
		gbcPanel2.fill = GridBagConstraints.BOTH;
		gbcPanel2.weightx = 1;
		gbcPanel2.weighty = 0;
		gbcPanel2.anchor = GridBagConstraints.NORTH;
		gbPanel2.setConstraints(btBut7, gbcPanel2);
		pnPanel2.add(btBut7);
		gbcPanel0.gridx = 1;
		gbcPanel0.gridy = 0;
		gbcPanel0.gridwidth = 19;
		gbcPanel0.gridheight = 19;
		gbcPanel0.fill = GridBagConstraints.BOTH;
		gbcPanel0.weightx = 1;
		gbcPanel0.weighty = 0;
		gbcPanel0.anchor = GridBagConstraints.NORTH;
		gbPanel0.setConstraints(pnPanel2, gbcPanel0);
		add(pnPanel2);

		/**
		 * This is the text field that the player will submit their answers in.
		 * It also sets up physical dimensions of the button.
		 */
		tfText2 = new JTextField(Question);
		gbcPanel2.gridx = 1;
		gbcPanel2.gridy = 7;
		gbcPanel2.gridwidth = 13;
		gbcPanel2.gridheight = 4;
		gbcPanel2.fill = GridBagConstraints.BOTH;
		gbcPanel2.weightx = 1;
		gbcPanel2.weighty = 0;
		gbcPanel2.anchor = GridBagConstraints.NORTH;
		gbPanel2.setConstraints(tfText2, gbcPanel2);
		pnPanel2.add(tfText2);

		/**
		 * This is the text box that will display the question to the player.
		 * 
		 * I'm thinking of putting a to string in here to print the question //
		 * which would allow us to check answers and have operable code.
		 */

		lbLabel4 = new JLabel("Let's Start the Game");
		gbcPanel2.gridx = 1;
		gbcPanel2.gridy = 3;
		gbcPanel2.gridwidth = 13;
		gbcPanel2.gridheight = 4;
		gbcPanel2.fill = GridBagConstraints.BOTH;
		gbcPanel2.weightx = 1;
		gbcPanel2.weighty = 1;
		gbcPanel2.anchor = GridBagConstraints.NORTH;
		gbPanel2.setConstraints(lbLabel4, gbcPanel2);
		pnPanel2.add(lbLabel4);
		gbcPanel0.gridx = 1;
		gbcPanel0.gridy = 0;
		gbcPanel0.gridwidth = 19;
		gbcPanel0.gridheight = 19;
		gbcPanel0.fill = GridBagConstraints.BOTH;
		gbcPanel0.weightx = 1;
		gbcPanel0.weighty = 0;
		gbcPanel0.anchor = GridBagConstraints.NORTH;
		gbPanel0.setConstraints(pnPanel2, gbcPanel0);
		add(pnPanel2);
	}

	/**
	 * @precondition Displays GUI and prompts user for input.
	 * @postcondition Reads in users answer as a string and sends the answer to
	 *                the questions class.
	 * 
	 *                This is my action listener and what actions should be
	 *                performed depending on what button is pressed and
	 */
	
	Number numb = new Number();
	int D = numb.numberGenerator2();
	Whichdecimalplace B = new Whichdecimalplace(D);
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		//Number numb = new Number();
		//int picker = numb.numberGenerator3();
		
		
		
		
	
		if (e.getActionCommand().equals("Submit Answer")) {
			String answer = tfText2.getText();
			btBut6.setText("Next Question");
			//if (2 == picker) {
			
				int a = Integer.parseInt(answer);
				
		//	if (0 == numb.numberGenerator4()){
				if (a == B.tenths()) {
					lbLabel4.setText("Your Answer is correct");
				} else {
					lbLabel4.setText("Your Answer is wrong. The correct answer is: " + B.tenths());}
				
		//	}
//		else if (1 == numb.numberGenerator4()){
//					if (a==B.hundredths()){
//						lbLabel4.setText("Correct");
//					}else{
//							lbLabel4.setText("Fail");}}
							
					
			
		
		

//			if (1 == picker) {
//				{
//					NumberInsert A = new NumberInsert(numb.numberGenerator());
//					A.run();
//					lbLabel4.setText(A.getQuestion());
//					if (A.getfinalAnswer().matches(answer)) {
//						// ADD ONE TO CORRECT ANSWER
//						lbLabel4.setText("CORRECT!");
//						
//					} else
//					// DURRRRR
//					lbLabel4.setText("INCORRECT");
//						
//				}
//			}
		}
		else if (e.getActionCommand().equals("See Score")) {
			System.out.println("Score button has been clicked");
			scoreWindow Window = new scoreWindow();
	///	scoreWindow.scoreText.setText(Quiz + "/" + Total);
		}

		else if (e.getActionCommand().equals("Start")) {
			
			// System.out.println("Start has been clicked");
//			if (1 == picker) {
//				{
//					NumberInsert A = new NumberInsert(numb.numberGenerator());
//					A.run();
//					lbLabel4.setText(A.getQuestion());
//				}

			//} else {
				//if (2 == picker) {
					//if (1 == numb.numberGenerator4()) {
						
						lbLabel4.setText("What number is in the tenths place: "
								+ B.StartingNumber());}
//					} else if (2 == numb.numberGenerator4()) {
//						lbLabel4.setText("What number is in the hundreths place: "
//							+ B.StartingNumber());
//					}
			//	}
			//}
						
						
						else if (e.getActionCommand().equals("Next Question")) {
						
					
								//	if (0 == numb.numberGenerator4()) {
										
										lbLabel4.setText("What number is in the tenths place: "
												+ B.StartingNumber()); 
//										
//								} else if (1 == numb.numberGenerator4()) {
//										lbLabel4.setText("What number is in the hundreths place: "
//											+ B.StartingNumber());
									//}
								
							

		} else if (e.getActionCommand().equals("Exit Game")) {
			System.exit(0);
			
		}
	}
}

